﻿using MQTTnet;
using MQTTnet.Protocol;
using MQTTnet.Server;
using System.Text;

namespace WinformMqttServer
{
    public class MqttService
    {
        public static MqttServer _mqttServer { get; set; }
        public static void PublishData(string data)
        {
            var msgBuiler = new MqttApplicationMessageBuilder();
            msgBuiler.WithTopic("topic_01");            
            msgBuiler.WithPayload(Encoding.Default.GetBytes(data));
            msgBuiler.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce);
            msgBuiler.WithRetainFlag(true);
            var msg = msgBuiler.Build();

            _mqttServer.InjectApplicationMessage(new InjectedMqttApplicationMessage(msg)
            {
                SenderClientId = "Server_01"
            }).GetAwaiter().GetResult();            
        }
    }
}
